<template>
  <div class="login">
    <div class="login-box">
      <div class="img-box">
        <img src="@/assets/logo.png" alt />
      </div>

      <el-form class="form-box" :model="formData" :rules="formRules" ref="form">
        <el-form-item prop="username">
          <el-input
            v-model="formData.username"
            prefix-icon="el-icon-user-solid"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="formData.password"
            prefix-icon="el-icon-lock"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="success" @click="loginClick">登录</el-button>
          <el-button type="warning" @click="resetClick">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      formData: {
        username: "",
        password: ""
      },
      formRules: {
        username: [
          { required: true, message: "请输入用户名称", trigger: "blur" },
          {
            min: 4,
            max: 10,
            message: "用户名只能是4-10位之间",
            trigger: "blur"
          }
        ],
        password: [
          { required: true, message: "密码没有输入", trigger: "blur" },
          {
            min: 4,
            max: 10,
            message: "密码只能是4-10位之间",
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    resetClick() {
      this.$refs.form.resetFields();
    },
    loginClick() {
      this.$refs.form.validate(async valid => {
        if (!valid) {
          return;
        } else {
          try {
            let res = await this.$http.post("login", this.formData);
            if (res.data.meta.status !== 200) {
              console.log(res);
              return this.$message.error("登录失败");
            } else {
              //将后台穿过来的token存放进session里
              this.$message.success("登录成功");
              sessionStorage.setItem("token", res.data.data.token);
              this.$router.push("/home");
              return;
            }
          } catch (err) {
            console.log(err);
          }
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.login {
  position: relative;
  background-color: #2b4b6b;
  height: 100vh;
  .login-box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    position: absolute;
    left: 50%;
    top: 200px;
    transform: translate(-50%);
    .img-box {
      position: absolute;
      width: 130px;
      height: 130px;
      padding: 20px;
      position: absolute;
      top: -70px;
      left: 50%;
      transform: translate(-50%);
      background-color: #fff;
      border-radius: 50%;
      img {
        width: 100%;
        height: 100%;
        border: 1px solid #fff;
        background: #2b4b6b;
        border-radius: 50%;
      }
    }
  }
  .form-box {
    width: 100%;
    position: absolute;
    top: 85px;
    .el-form-item {
      box-sizing: border-box;
      padding: 0 10px;
    }
    //设置最后一个buttons的分布
    :last-child {
      display: flex;
      justify-content: flex-end;
    }
  }
}
</style>
